//
// C++ Implementation: gpl
//
// Description: Print strings from Program Memory to Hardware Serial port.
//
//
// Author: Andrey Sharoyko <vanyamboe@gmail.com>, (C) 2011
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "dumpdebug.h"
#include "printpgm.h"

void printPgm(prog_char* pstr)
{
  char *p = pstr;
  char ch;
  do {
    ch = pgm_read_byte(p);
    if (ch)
      Serial.print(ch);
    ++p;
  } while(ch);
}

void printPgm(const char** pstr)
{
  printPgm((prog_char*) pgm_read_word(pstr));
}

void printlnPgm(prog_char* pstr)
{
  printPgm(pstr);
  Serial.println();
}

void printlnPgm(const char** pstr)
{
  printlnPgm((prog_char*) pgm_read_word(pstr));
}

char halfByteToHex(unsigned char hb)
{
  return (hb > 9) ? (hb -10) + 'A' : hb + '0';
}

void dumpHexByte(unsigned char b)
{
  Serial.print(halfByteToHex(b >> 4));
  Serial.print(halfByteToHex(b & 0x0F));
}

void dumpHexWord(unsigned short s)
{
    short ss = (s >> 8) & 0xFF;
    dumpHexByte(ss);
    dumpHexByte(s);
}

void dumpBinByte(unsigned char b)
{
  unsigned char mask = 0x80;
  for(char n=0; n<8; ++n) {
    Serial.print((b & mask) ? '1' : '0');
    mask >>= 1;
  }
}

